<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/7/19 20:26
 */

namespace module\user\front;

use app\model\Module;
use mof\annotation\Description;

class Config extends \mof\front\Config
{
    #[Description('基础设置')]
    public function groupBase(array $values): array
    {
        return [
            [
                "label"   => "手机号",
                "type"    => "checkbox",
                "prop"    => "mobile_feature",
                "value"   => $values['mobile_feature'] ?? [],
                "options" => [
                    ['caption' => '注册验证', 'value' => 'register'],
                    ['caption' => '短信登录', 'value' => 'login'],
                    ['caption' => '找回密码', 'value' => 'forget'],
                    ['caption' => '允许换绑', 'value' => 'bind'],
                ],
                "intro"   => "选择开启用户手机号的具体用途。启用功能前，请配置短信服务（系统-系统设置-短信设置）",
                "_extra"  => ['client_cfg' => 1],
            ],
            [
                "label"   => "邮箱",
                "type"    => "checkbox",
                "prop"    => "email_feature",
                "value"   => $values['email_feature'] ?? [],
                "options" => [
                    ['caption' => '注册验证', 'value' => 'register'],
                    ['caption' => '找回密码', 'value' => 'forget'],
                    ['caption' => '允许换绑', 'value' => 'bind'],
                ],
                "intro"   => "选择开启用户邮箱账号的具体用途。启用功能前，请配置邮箱服务（系统-系统设置-邮件设置）",
                "_extra"  => ['client_cfg' => 1],
            ],
            [
                "label"  => "默认头像",
                "type"   => "upload:image",
                "prop"   => "avatar",
                "value"  => $values['avatar'] ?? '',
                "intro"  => "用户注册后的默认头像",
                "_extra" => ['client_cfg' => 1],
            ],
            [
                "label" => "初始积分",
                "type"  => "input-number",
                "prop"  => "register_score",
                "value" => $values['register_score'] ?? '',
                "intro" => "用户注册后，系统赠送的积分",
            ],
        ];
    }

    #[Description('奖励设置')]
    public function groupReward(array $values): array
    {
        return [
            [
                "label"  => "邀请开关",
                "type"   => "switch",
                "prop"   => "invite_enabled",
                "value"  => $values['invite_enabled'] ?? false,
                "intro"  => "是否开启邀请还有赠送积分的功能",
                "_extra" => ['client_cfg' => 1],
            ],
            [
                "label"    => "邀请奖励",
                "type"     => "input-number",
                "prop"     => "invite_reward",
                "value"    => $values['invite_reward'] ?? false,
                "intro"    => "邀请好友成功后，奖励多少积分",
                '_visible' => 'invite_enabled=true',
                "_extra"   => ['client_cfg' => 1],
            ],
            [
                'prop'   => 'sign_in_enabled',
                'label'  => '签到开关',
                'type'   => 'switch',
                'value'  => $values['sign_in_enabled'] ?? false,
                'intro'  => "是否开启签到功能",
                "_extra" => ['client_cfg' => 1],
            ],
            [
                'prop'     => 'sign_in_scores',
                'label'    => '签到奖励',
                'type'     => 'input',
                'value'    => $values['sign_in_scores'] ?? '1,2,3,4,5,6,7',
                'intro'    => "签到奖励积分，按天设置奖励积分，最多7个（即最多7天一轮）<br>例如：1,3,5,7,9,12,20。表示第一天奖励1分，第二天奖励3分，以此类推",
                '_visible' => 'sign_in_enabled=true',
                "_extra"   => ['client_cfg' => 1],
            ],
        ];
    }

    #[Description('消费设置')]
    public function groupConsumption(array $values): array
    {
        $consumptionList = $this->getConsumptionList();
        return [
            [
                'prop'    => 'consumption_user',
                'label'   => '普通用户',
                'type'    => 'input-dict',
                'value'   => empty($values['consumption_user']) ? (object)[] : $values['consumption_user'],
                'options' => array_map(function ($item) {
                    return [
                        'prepend' => $item['label'],
                        'key'     => $item['name'],
                        'append'  => '积分/次'
                    ];
                }, $consumptionList),
                'intro'   => '普通会员使用功能时的单次积分消耗，0表示免费使用。',
                "_extra"  => ['client_cfg' => 1],
            ],
            [
                'prop'    => 'consumption_vip',
                'label'   => 'VIP用户',
                'type'    => 'input-dict',
                'value'   => empty($values['consumption_vip']) ? (object)[] : $values['consumption_vip'],
                'options' => array_map(function ($item) {
                    return [
                        'prepend' => $item['label'],
                        'key'     => $item['name'],
                        'append'  => '积分/次'
                    ];
                }, $consumptionList),
                'intro'   => 'VIP会员使用功能时的单次积分消耗，0表示免费使用。',
                "_extra"  => ['client_cfg' => 1],
            ]
        ];
    }

    /**
     * 给短信配置模块提供短信模板
     * @return array[]
     */
    public function smsTemplate(array $values): array
    {
        return [
            'label'   => '用户短信模板',
            'type'    => 'input-dict',
            'value'   => $values ?: (object)[],
            'options' => [
                [
                    'prepend' => '登录验证码',
                    'key'     => 'login',
                ], [
                    'prepend' => '注册验证码',
                    'key'     => 'register',
                ], [
                    'prepend' => '忘记密码验证码',
                    'key'     => 'forget',
                ], [
                    'prepend' => '绑定手机号验证码',
                    'key'     => 'bind',
                ]
            ],
            'intro'   => '验证码类短信模板需要包含验证码和时间参数。'
        ];
    }

    private function getConsumptionList()
    {
        //获取模块
        $modules = Module::enabledModules();
        return array_reduce($modules, function ($result, $module) {
            $name = $module['name'];
            $file = \mof\Module::path($name) . 'front' . DIRECTORY_SEPARATOR . 'Config.php';
            if (is_file($file)) {
                $className = "\\module\\{$name}\\front\\Config";
                if (class_exists($className) && method_exists($className, 'consumption')) {
                    $config = new $className;
                    $data = $config->consumption();
                    //给data数组，加上module 字段
                    array_walk($data, function (&$item) use ($name) {
                        $item['module'] = $name;
                    });
                    //$data合并到$result
                    $result = array_merge($result, $data);
                }
            }
            return $result;
        }, []);
    }
}